package com.xy.view.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.a520it.generalutils.R;

/**
 * Created by John on 2016/12/1.
 */

public class ZimaViewF extends View {

    private Paint paint;
    private int radius;
    private int maxNum;
    private int topNum;
    private int centreLNum;
    private int centreRNum;
    private int BottomLNum;
    private int BottomRNum;
    private int centreLRx;
    private int centreLRy;
    private int bottomLRx;
    private int bottomLRy;
    private int w;

    public ZimaViewF(Context context) {
        this(context, null);
    }

    public ZimaViewF(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZimaViewF(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(0xffffffff);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setDither(true);
        maxNum = 1000;
        topNum = 500;
        centreLNum = centreRNum = 500;
        BottomLNum = BottomRNum = 500;
        w = dp2px(3);  //间隔
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        int width = measureWidth(widthMeasureSpec);
        setMeasuredDimension(width, height);
    }

    private int measureWidth(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;
        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else {
            width = dp2px(400);
        }
        return width;
    }

    private int measureHeight(int heightMeasureSpec) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;
        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else {
            height = dp2px(300);
        }
        return height;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        radius = Math.min(getMeasuredHeight(), getMeasuredWidth()) / 2 - dp2px(60);  //5边形的每个角到中心的距离
        canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);  //移到view的中心
        centreLRx = (int) (radius * Math.cos(Math.toRadians(18)));//90 - 72
        centreLRy = (int) (radius * Math.sin(Math.toRadians(18)));
        bottomLRx = (int) (radius * Math.sin(Math.toRadians(36)));
        bottomLRy = (int) (radius * Math.cos(Math.toRadians(36)));
        drawLine(canvas);
        drawView(canvas);
        drawOutText(canvas);
        drawCentreText(canvas);
    }

    private void drawCentreText(Canvas canvas) {
        canvas.save();
        Paint textP = new Paint();
        textP.setColor(0xffffffff);
        textP.setTextSize(radius /4);
        textP.setStrokeWidth(dp2px(2));
        String currentNum = (int)((topNum + centreLNum +centreRNum +BottomLNum + BottomRNum) * 0.2)+"";
        Rect rect = new Rect();
        textP.getTextBounds(currentNum,0,currentNum.length(),rect);
        //第三个参数是文字的基准线位置
        Paint.FontMetrics fm = textP.getFontMetrics();
        float textCenterVerticalBaselineY = rect.height()/2 - fm.descent + (fm.bottom - fm.top) / 2;
        canvas.drawText(currentNum,-rect.width()/2,textCenterVerticalBaselineY/2,textP);
        canvas.restore();
    }

    private String[] str = new String[]{"top", "CentreLeft", "CentreRight", "BottomLeft", "BottomRight"};

    private void drawOutText(Canvas canvas) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        canvas.save();
        Paint strP = new Paint();
        strP.setTextSize(sp2px(12));
        strP.setStyle(Paint.Style.FILL);
        strP.setColor(0xffffffff);
        strP.setAlpha(0x80);

        int centreLRxW = (int) ((radius + w) * Math.cos(Math.toRadians(18)));//90 - 72
        int centreLRyW = (int) ((radius + w) * Math.sin(Math.toRadians(18)));
        int bottomLRxW = (int) ((radius + w) * Math.sin(Math.toRadians(36)));
        int bottomLRyW = (int) ((radius + w) * Math.cos(Math.toRadians(36)));


        //top
        canvas.drawBitmap(bitmap, -bitmap.getWidth() / 2, -radius - w - bitmap.getHeight() - dp2px(12), strP);
        canvas.drawText(str[0], -strP.measureText(str[0]) / 2, -radius - w - dp2px(8), strP);
        //centreL R
        canvas.drawBitmap(bitmap, -centreLRxW - bitmap.getWidth(),-centreLRyW - dp2px(4) - bitmap.getHeight() / 2, strP);
        canvas.drawText(str[1], -centreLRxW - strP.measureText(str[1]), -centreLRyW + bitmap.getHeight() / 2, strP);

        canvas.drawBitmap(bitmap, centreLRxW+Math.abs(strP.measureText(str[2])-bitmap.getWidth())/2, -centreLRyW - dp2px(4)- bitmap.getHeight() / 2, strP);
        canvas.drawText(str[2], centreLRxW , -centreLRyW + bitmap.getHeight() / 2, strP);
        //bottom l r
        Rect bl = new Rect();
        paint.getTextBounds(str[3], 0, str[3].length(), bl);
        canvas.drawBitmap(bitmap, -bottomLRxW - bitmap.getWidth() / 2, bottomLRyW, strP);
        canvas.drawText(str[3], -bottomLRxW - bitmap.getWidth() / 2 - bl.width() / 2, bottomLRyW + bitmap.getHeight() + bl.height(), strP);
        Rect br = new Rect();
        paint.getTextBounds(str[4], 0, str[4].length(), br);
        canvas.drawBitmap(bitmap, bottomLRxW, bottomLRyW, strP);
        canvas.drawText(str[4], bottomLRxW - br.width() / 2, bottomLRyW + bitmap.getHeight() + br.height(), strP);
        canvas.restore();
    }

    private void drawView(Canvas canvas) {
        canvas.save();
        Paint viewP = new Paint();
        viewP.setStyle(Paint.Style.FILL);
        viewP.setColor(0xffffffff);
        viewP.setFlags(Paint.ANTI_ALIAS_FLAG);
        viewP.setDither(true);
        viewP.setAlpha(0x50);
        Path path = new Path();
        path.moveTo(0, -topNum / (maxNum * 1.0f) * radius);
        path.lineTo(centreLRx * centreRNum / (maxNum * 1.0f), -centreLRy * centreRNum / (maxNum * 1.0f));
        path.lineTo(bottomLRx * BottomRNum / (maxNum * 1.0f), bottomLRy * BottomRNum / (maxNum * 1.0f));
        path.lineTo(-bottomLRx * BottomLNum / (maxNum * 1.0f), bottomLRy * BottomLNum / (maxNum * 1.0f));
        path.lineTo(-centreLRx * centreLNum / (maxNum * 1.0f), -centreLRy * centreLNum / (maxNum * 1.0f));
        path.close();
        canvas.drawPath(path, viewP);
        canvas.restore();
    }

    private void drawLine(Canvas canvas) {
        canvas.save();
        paint.setStrokeWidth(dp2px(1));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAlpha(0x50);
        Path path = new Path();
        Point centre = new Point(0, 0);
        Point top = new Point(0, -radius);
        Point centreL = new Point(-centreLRx, -centreLRy);
        Point centreR = new Point(centreLRx, -centreLRy);
        Point bottomL = new Point(-bottomLRx, bottomLRy);   //360/5/2
        Point bottomR = new Point(bottomLRx, bottomLRy);
        path.moveTo(centre.x, centre.y);
        path.lineTo(top.x, top.y);
        path.lineTo(centreR.x, centreR.y);
        path.lineTo(bottomR.x, bottomR.y);
        path.lineTo(bottomL.x, bottomL.y);
        path.lineTo(centreL.x, centreL.y);
        path.lineTo(top.x, top.y);
        canvas.drawPath(path, paint);
        canvas.drawLine(centre.x, centre.y, centreL.x, centreL.y, paint);
        canvas.drawLine(centre.x, centre.y, centreR.x, centreR.y, paint);
        canvas.drawLine(centre.x, centre.y, bottomL.x, bottomL.y, paint);
        canvas.drawLine(centre.x, centre.y, bottomR.x, bottomR.y, paint);
        canvas.restore();
    }

    private int dp2px(int dp) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }

    private int sp2px(int sp) {
        float density = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * density + 0.5f);
    }

}
